package com.xinxin.leetcode.problem532;

import java.util.Arrays;

/**
 * @author ShiXinXin
 * @date 2020-01-15 9:15
 */
class Solution {
    public int findPairs(int[] nums, int k) {
        Integer temp = null;
        int sum = 0;
        Arrays.sort(nums);
        int jIndex = 1;
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = jIndex; j < nums.length; j++) {
                if (nums[i] + k == nums[j]) {
                    if (temp == null || nums[i] != temp) {
                        sum++;
                        temp = nums[i];
                        jIndex = j + 1;
                        break;
                    }
                } else {
                    if (nums[i] + k < nums[j]) {
                        jIndex = Math.max(i + 2, j);
                        break;
                    }
                }
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        int[] nums = {1, 3, 1, 5, 4};
        System.out.println(new Solution().findPairs(nums, 0));
    }
}
